package com.taylor.core.bean.util;

import java.sql.Time;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.sql.Date;

/**
 * LessonEvent関連のユーティリティクラスです。
 * 
 * @author 西田正洋
 */
public class LessonEventUtil {
	/** バランスの最大値。 */
	public static final int MAX_BALANCE = 30;
	public static final int TIME_LIMIT = 17;
	public static final String LE_USER_ERR_PAGE = "LEUserErrorPage";

	private static final String[] ARR_BALANCE = { "C0", "C1", "C2", "C3", "C4",
			"C5", "C6", "C7", "C8", "C9", "D0", "D1", "D2", "D3", "D4", "D5",
			"D6", "D7", "D8", "D9", "E0", "E1", "E2", "E3", "E4", "E5", "E6",
			"E7", "E8", "E9" };

	public static final int HALF_DAY = -1;
	public static final String STRING_HALF_DAY = "半";

	/** 「男性」sex_kind male (lesson_instructor.li_sex) */
	public static final int LI_SEX_KIND_MALE = 1;
	/** 「女性」sex_kind female (lesson_instructor.li_sex) */
	public static final int LI_SEX_KIND_FEMALE = 2;

	/**
	 * get Date Object from Year, Month, Day
	 * 
	 * @param intYear
	 * @param intMonth
	 * @param intDay
	 * @return
	 */
	public static Date getDateFromYearMonthDay(Integer intYear,
			Integer intMonth, Integer intDay) {
		Date date = null;
		if (intYear != null && intMonth != null && intDay != null) {
			/*
			 * Calendar cl = Calendar.getInstance(); cl.set(intYear.intValue(),
			 * intMonth.intValue(), intDay.intValue()); date = cl.getTime();
			 */
			String sYear = intYear.toString();
			String sMonth = intMonth.toString();
			if (intMonth.intValue() < 10) {
				sMonth = "0" + sMonth;
			}
			String sDay = intDay.toString();
			if (intDay.intValue() < 10) {
				sDay = "0" + sDay;
			}
			date = Date.valueOf(sYear + "-" + sMonth + "-" + sDay);
		}
		return date;
	}

	public static boolean validateDate(Integer intYear, Integer intMonth,
			Integer intDay) {
		Calendar cal = new GregorianCalendar(intYear.intValue(),
				intMonth.intValue() - 1, intDay.intValue());// monthは0スタート
		// Calendar cal = Calendar.getInstance();
		// cal.setLenient(false); //日付の厳密なチェックを行う。
		// cal.set(intYear.intValue(), intMonth.intValue(), intDay.intValue());
		cal.setLenient(false);

		try {
			cal.getTime();

			int y = cal.get(Calendar.YEAR);
			int m = cal.get(Calendar.MONTH);
			int d = cal.get(Calendar.DATE);

			if (cal.get(Calendar.YEAR) != intYear.intValue()
					|| cal.get(Calendar.MONTH) != intMonth.intValue() - 1
					|| cal.get(Calendar.DATE) != intDay.intValue()) {
				return false;
			}
		} catch (IllegalArgumentException e) {
			return false;
		}
		return true;
	}

	public static Time getTimeFromHour(Integer intHour) {
		Time tm = null;
		if (intHour != null) {
			String sHour = null;
			if (intHour.intValue() >= 10) {
				sHour = intHour.toString();
			} else {
				sHour = "0" + intHour.toString();
			}
			tm = Time.valueOf(sHour + ":00:00");
		}
		return tm;
	}

	public static Time getTimeFromHourMinute(Integer intHour, Integer intMinute) {
		Time tm = null;
		String sHour = "00";
		String sMinute = "00";
		if (intHour != null) {
			sHour = null;
			if (intHour.intValue() >= 10) {
				sHour = intHour.toString();
			} else {
				sHour = "0" + intHour.toString();
			}
		}
		if (intMinute != null) {
			sMinute = null;
			if (intMinute.intValue() >= 10) {
				sMinute = intMinute.toString();
			} else {
				sMinute = "0" + intMinute.toString();
			}
		}
		tm = Time.valueOf(sHour + ":" + sMinute + ":00");
		return tm;
	}

	/**
	 * 「現在の年」を表すStringを返します。
	 * 
	 * @return 今年の文字列表現
	 */
	public static String getNowYear() {
		Calendar calendar = Calendar.getInstance();
		return Integer.toString(calendar.get(Calendar.YEAR));
	}

	public static String getLeftPad2Zero(String str) {
		String tmp = "0" + str;
		tmp = tmp.substring(tmp.length() - 2);
		return tmp;
	}

	/**
	 * 「現在の月」を表すStringを返します。
	 * 
	 * @return 今月(1から12の値)の文字列表現
	 */
	public static String getNowMonth() {
		Calendar calendar = Calendar.getInstance();
		return getLeftPad2Zero(Integer
				.toString(calendar.get(Calendar.MONTH) + 1));
	}

	/**
	 * 「現在の日」を表すStringを返します。
	 * 
	 * @return 今日の文字列表現
	 */
	public static String getNowDay() {
		Calendar calendar = Calendar.getInstance();
		return getLeftPad2Zero(Integer.toString(calendar.get(Calendar.DATE)));
	}

	/**
	 * 「現在の時間」を表すStringを返します。
	 * 
	 * @return 現在時刻の時間の文字列表現
	 */
	public static String getNowHour() {
		Calendar calendar = Calendar.getInstance();
		return getLeftPad2Zero(Integer.toString(calendar
				.get(Calendar.HOUR_OF_DAY)));
	}

	/**
	 * year年の２月内日数を返す
	 * 
	 * @param year
	 *            年
	 * @return ニ月内日数
	 */
	public static int daysInFebruary(int year) {
		// 4でも４００でも割り切れない年を除いて、
		// どんな4で割り切れる年の２月にも29日があります
		return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29
				: 28);
	}

	/**
	 * year年のmonth月内日数を返す
	 * 
	 * @param month
	 *            月
	 * @param year
	 *            年
	 * @return 月内日数
	 */
	public static int daysInMonth(int month, int year) {
		int dayofmonth = 0;
		switch (month) {
		case 1:
			dayofmonth = 31;
			break;
		case 2:
			dayofmonth = daysInFebruary(year);
			break;
		case 3:
			dayofmonth = 31;
			break;
		case 4:
			dayofmonth = 30;
			break;
		case 5:
			dayofmonth = 31;
			break;
		case 6:
			dayofmonth = 30;
			break;
		case 7:
			dayofmonth = 31;
			break;
		case 8:
			dayofmonth = 31;
			break;
		case 9:
			dayofmonth = 30;
			break;
		case 10:
			dayofmonth = 31;
			break;
		case 11:
			dayofmonth = 30;
			break;
		case 12:
			dayofmonth = 31;
			break;
		}

		return dayofmonth;
	}

	public static String getyyyyMMddHHmmssFromCalendar(Calendar cal) {

		String yyyyMMddHHmmss_ = String.valueOf(cal.get(Calendar.YEAR) - 1900)
				+ getLeftPad2Zero(String.valueOf(cal.get(Calendar.MONTH) + 1))
				+ getLeftPad2Zero(String.valueOf(cal.get(Calendar.DATE)))
				+ getLeftPad2Zero(String.valueOf(cal.get(Calendar.HOUR_OF_DAY)))
				+ getLeftPad2Zero(String.valueOf(cal.get(Calendar.MINUTE)))
				+ getLeftPad2Zero(String.valueOf(cal.get(Calendar.SECOND)));

		return yyyyMMddHHmmss_;
	}

}
